package org.lanqiao.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component("logAnnotation")
@Aspect //声明该类是一个前置通知
public class LogBeforeAnnotation {

    //前置通知
    @Before("execution(public * addStudent(..))")  //属性:定义切点
    public void myBefore(JoinPoint jp){
        System.out.println("【注解形式】--------前置通知：目标对象："+jp.getTarget()+",方法名:"+jp.getSignature().getName()+",参数列表："+ Arrays.toString(jp.getArgs()));
    }

    //后置通知
    @AfterReturning("execution(public * addStudent(..))")
    public void myAfter(JoinPoint jp) {
        System.out.println("【注解形式】---------后置通知：目标对象：" + jp.getTarget() + ",方法名:" + jp.getSignature().getName() + ",参数列表：" + Arrays.toString(jp.getArgs()));
    }


        //环绕通知 参数ProceedingJoinPoint
        @Around("execution(public * addStudent(..))")
        public void myAround(ProceedingJoinPoint jp){
            //方法之前：前置通知
        try {

            //方法执行：
            jp.proceed();
//            方法执行之后：后置通知
            System.out.println("【环绕】方法之前之后：后置通知");
        }catch (Throwable e){
           //发生异常时，：异常通知
            System.out.println("【环绕】发生异常时：异常通知");
        }finally {

            //最终通知
            System.out.println("【环绕】最终通知");
        }

            System.out.println("【环绕】【注解形式】--------环绕通知");
        }


        //异常通知
        @AfterThrowing(value = "execution(public * addStudent(..))",throwing = "e")
        public void myException(){
            System.out.println("【注解形式】--------异常通知");
        }
        //最终通知
        @After("execution(public * addStudent(..))")
    public void myAfter(){
            System.out.println("【after注解形式】--------异常通知");
        }



}
